package biolean.files;

import java.util.ArrayList;
import java.util.List;

public class FileRegister
{
	
	protected class RegNode
	{
		protected Class<?> mBiofileClass;
		protected String   mFileTypeName;
		protected String[] mExtensions;
		public RegNode(Class<?> c, String name,  String[] ext)
		{
			mBiofileClass = c;
			mFileTypeName = name;
			mExtensions= ext;
		}
		
	}
	
	private static List<RegNode> mClasses = new ArrayList<FileRegister.RegNode>();
	
	public void register(Class<?> bioFileclas, String[] extensions, String name)
	{
		mClasses.add(new RegNode(bioFileclas, name, extensions));
	}
	
	public String findTypeName(Class<?> cls)
	{
		for (RegNode node : mClasses)
		{
			if (node.mBiofileClass.equals( cls) )
				return node.mFileTypeName;
		}
		return null;
	}
	
	public Class<?> findClass(String fileTypeName)
	{
		for (RegNode node : mClasses)
		{
			if (node.mFileTypeName.equals( fileTypeName) )
				return node.mBiofileClass;
		}
		return null;
	}
	
	public Class<?> findClassByExt(String extension)
	{
		for (RegNode node : mClasses)
		{
			for (int i = 0 ; i < node.mExtensions.length ; i++)
			{
				if (node.mExtensions[i].equals( extension ) )
					return node.mBiofileClass;
			}
		}
		return null;
	}
	
	public String[] getFileTypeNames()
	{
		String[] result = new String[mClasses.size()];
		for (int i = 0; i <  mClasses.size() ; i++)
		{
			result[i] = mClasses.get(i).mFileTypeName;
		}
		return result;
	}
	
	public String[] getExtensions(int index)
	{
		return mClasses.get(index).mExtensions;
	}
	
	public String getTypeName(int index)
	{
		return mClasses.get(index).mFileTypeName;
	}
	
	public Class<?> getBioFileClass(int index)
	{
		return mClasses.get(index).mBiofileClass;
	}
	
	public String findExtension(Class<?> cls)
	{
		for (RegNode node : mClasses )
		{
			if (cls.equals( node.mBiofileClass ) && node.mExtensions.length > 0)
				return node.mExtensions[0];
		}
		return null;
	}
}
